﻿using System;
using System.Collections.Generic;
using System.Text;
using Peekaboo2008.model;
using System.Data;
using System.Data.SqlClient;
using Peekaboo2008.util;

namespace Peekaboo2008.dao
{
    class HangTonDAO
    {
        public static int getSoLuongTonKhoOfHangHoaByMaHang(String _Id)
        {
            int _rs = 0;
            int i = 0;
            SqlParameter[] param = new SqlParameter[1];
            param[i] = new SqlParameter("Ma_hang", SqlDbType.Int);
            param[i++].Value = _Id;
            DataTable _dt = DataUtil.executeStore("sp_HangTon_GetSoLuongTonKhoOfHangHoaByMaHang", param);
            try
            {
                _rs = Int32.Parse(_dt.Rows[0][0].ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace.ToCharArray());
            }
            return _rs;
        }

        public static Boolean updateSoLuongTonKhoOfBanHangByMaChiNhanh_MaHang(String _maChiNhanh, String _MaHangHoa,String _soLuong)
        {
            SqlParameter[] param = new SqlParameter[3];
            int i = 0;
            param[i] = new SqlParameter("Ma_chi_nhanh", SqlDbType.Int);
            param[i++].Value = _maChiNhanh;
            param[i] = new SqlParameter("Ma_hang_hoa", SqlDbType.Int);
            param[i++].Value = _MaHangHoa;
            param[i] = new SqlParameter("So_luong", SqlDbType.Float);
            param[i++].Value = _soLuong;
            return DataUtil.executeNonStore("sp_HangTon_UpdateSoLuongTonKhoOfBanHangByMaChiNhanh_MaHang", param);

        }
        public static Boolean revertSoLuongTonKhoOfBanHangByMaChiNhanh_MaHang(String _maChiNhanh, String _MaHangHoa, String _soLuong)
        {
            SqlParameter[] param = new SqlParameter[3];
            int i = 0;
            param[i] = new SqlParameter("Ma_chi_nhanh", SqlDbType.Int);
            param[i++].Value = _maChiNhanh;
            param[i] = new SqlParameter("Ma_hang_hoa", SqlDbType.Int);
            param[i++].Value = _MaHangHoa;
            param[i] = new SqlParameter("So_luong", SqlDbType.Float);
            param[i++].Value = _soLuong;
            return DataUtil.executeNonStore("sp_HangTon_RevertSoLuongTonKhoOfBanHangByMaChiNhanh_MaHang", param);

        }
        public static Boolean updateSoLuongTonKhoOfTraHangByMaChiNhanh_MaHang(String _maChiNhanh, String _MaHangHoa, String _soLuong)
        {
            SqlParameter[] param = new SqlParameter[3];
            int i = 0;
            param[i] = new SqlParameter("Ma_chi_nhanh", SqlDbType.Int);
            param[i++].Value = _maChiNhanh;
            param[i] = new SqlParameter("Ma_hang_hoa", SqlDbType.Int);
            param[i++].Value = _MaHangHoa;
            param[i] = new SqlParameter("So_luong", SqlDbType.Float);
            param[i++].Value = _soLuong;
            return DataUtil.executeNonStore("sp_HangTon_UpdateSoLuongTonKhoOfTraHangByMaChiNhanh_MaHang", param);

        }

        public static int SearchByMaChiNhanh_Mahang(String _maChiNhanh, String _MaHangHoa)
        {
            int rs = -1;
            SqlParameter[] param = new SqlParameter[3];
            int i = 0;
            param[i] = new SqlParameter("Ma_chi_nhanh", SqlDbType.Int);
            param[i++].Value = _maChiNhanh;
            param[i] = new SqlParameter("Ma_hang_hoa", SqlDbType.Int);
            param[i++].Value = _MaHangHoa;
            param[i] = new SqlParameter("ID", SqlDbType.Int);
            param[i].Direction = ParameterDirection.Output;
            DataTable tb =  DataUtil.executeStore("sp_HangTon_SearchByMaChiNhanh_Mahang", param);
            try { rs = int.Parse(param[i].Value.ToString()); }
            catch (Exception e) { Console.WriteLine(e.StackTrace); }
            return rs;
        }
        public static Boolean updateSoLuongTonKhoOfNhapHangByMaChiNhanh_MaHang(String _maChiNhanh, String _MaHangHoa, String _soLuong)
        {
            SqlParameter[] param = new SqlParameter[3];
            int i = 0;
            param[i] = new SqlParameter("Ma_chi_nhanh", SqlDbType.Int);
            param[i++].Value = _maChiNhanh;
            param[i] = new SqlParameter("Ma_hang_hoa", SqlDbType.Int);
            param[i++].Value = _MaHangHoa;
            param[i] = new SqlParameter("So_luong", SqlDbType.Float);
            param[i++].Value = _soLuong;
            return DataUtil.executeNonStore("sp_HangTon_UpdateSoLuongTonKhoOfNhapHangNCCByMaChiNhanh_MaHang", param);

        }

        /*
         * Su dung ben phieu nhap xuat 
         * */
        public static String InsertTonKhoOfNhapHangByMaChiNhanh_MaHang(HangTonModel _Model)
        {
            String returnID = "";
            SqlParameter[] param = new SqlParameter[7];
            int i = 0;

            param[i] = new SqlParameter("Ma_hang", SqlDbType.Int);
            param[i++].Value = _Model.MaHang;
            param[i] = new SqlParameter("So_luong", SqlDbType.Float);
            param[i++].Value = _Model.SoLuong;
            param[i] = new SqlParameter("Ghi_chu", SqlDbType.NVarChar);
            param[i++].Value = _Model.GhiChu;
            param[i] = new SqlParameter("Gia_nhap_cuoi", SqlDbType.Float);
            param[i++].Value = _Model.GiaNhapCuoi;
            param[i] = new SqlParameter("Gia_xuat_cuoi", SqlDbType.Float);
            param[i++].Value = _Model.GiaXuatCuoi;
            param[i] = new SqlParameter("Ma_chi_nhanh", SqlDbType.Int);
            param[i++].Value = _Model.MaChiNhanh;
            param[i] = new SqlParameter("OutID", SqlDbType.Int);
            param[i].Direction = ParameterDirection.Output;

            DataUtil.executeNonStore("sp_HangTon_InsertTonKhoOfNhapHangByMaChiNhanh_MaHang", param);

            returnID = param[i].Value.ToString();
            return returnID;
        }
    }
}
